package com.daqian.mall.config;

import com.daqian.mall.orm.Mapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import tk.mybatis.spring.mapper.MapperScannerConfigurer;

import java.util.Properties;

@Configuration
public class MyBatisMapperScannerConfig {


    private static final String[] BASE_PACKAGE = {
            "com.**.mapper",  "org.**.mapper", "cn.**.mapper", "net.**.mapper","me.**.mapper",
            "com.**.dao", "cn.**.dao", "org.**.dao", "net.**.dao","me.**.dao",
            //"com.**.repository", "cn.**.repository", "org.**.repository", "net.**.repository","me.**.repository",
    };

    private static String getBasePackage(){
        StringBuilder basePackageStr = new StringBuilder();
        for (String s : BASE_PACKAGE) {
            basePackageStr.append(s).append(",");
        }
        return basePackageStr.toString();
    }

    @Bean
    public static MapperScannerConfigurer mapperScannerConfigurer() {

        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");

        mapperScannerConfigurer.setBasePackage(getBasePackage());
        Properties properties = new Properties();
        properties.setProperty("mappers", Mapper.class.getName());
        properties.setProperty("notEmpty", "false");
        properties.setProperty("IDENTITY", "MYSQL");
        //这里使用的通用Mapper的MapperScannerConfigurer，所有有下面这个方法
        mapperScannerConfigurer.setProperties(properties);
        return mapperScannerConfigurer;
    }
}
